<!-- 
 * Tournament bracket system for use in scenarios 
 * like sports, computer gaming or other games.
 * 
 * @link http://www.toggymedy.com
 * @license  GNU/GPL, ( http://www.gnu.org/licenses/gpl.txt )
 * @authors Team Excellence

 * The tournament bracket is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * Last update: 2009-11-27
 -->

<?php
	include_once('../dbconnect.php');
	$turn = $_POST['turnid'];
	$data = mysql_query("SELECT turnering.startet from turnering where turnering.turnid = $turn and startet = 1");
	$startet = mysql_num_rows($data);
	$q1 = mysql_query("SELECT turnering.spill, kamp.kampid, kamp.laga, kamp.lagb, kamp.resa, kamp.resb, laga.lagnavn as 'Lag 1', lagb.lagnavn as 'Lag 2'
							FROM kamp  INNER JOIN turnering ON turnering.turnid = kamp.turnid
							INNER JOIN lag laga ON laga.lagid = kamp.laga
							INNER JOIN lag lagb ON lagb.lagid = kamp.lagb
							where kamp.turnid = $turn
							GROUP BY turnering.spill, kamp.kampid
							LIMIT 0, 4") or die(mysql_error());
	$q2 =	mysql_query("SELECT turnering.spill, kamp.kampid, kamp.laga, kamp.lagb, kamp.resa, kamp.resb, laga.lagnavn as 'Lag 1', lagb.lagnavn as 'Lag 2'
							FROM kamp INNER JOIN turnering ON turnering.turnid = kamp.turnid
							INNER JOIN lag laga ON laga.lagid = kamp.laga
							INNER JOIN lag lagb ON lagb.lagid = kamp.lagb
							where kamp.turnid = $turn
							GROUP BY turnering.spill, kamp.kampid							
							LIMIT 4, 2") or die(mysql_error());
	$q3 =	mysql_query("SELECT turnering.spill, kamp.kampid, kamp.laga, kamp.lagb, kamp.resa, kamp.resb, laga.lagnavn as 'Lag 1', lagb.lagnavn as 'Lag 2'
							FROM kamp INNER JOIN turnering ON turnering.turnid = kamp.turnid
							INNER JOIN lag laga ON laga.lagid = kamp.laga
							INNER JOIN lag lagb ON lagb.lagid = kamp.lagb
							where kamp.turnid = $turn 
							GROUP BY turnering.spill, kamp.kampid
							LIMIT 6, 1") or die(mysql_error());
?>
						
	<form name='velgturn' method='post' action=' <?php echo "$_SERVER[PHP_SELF]" ?>?side=viskamper'>
<?php
		echo "<table>";
			echo "<tr>";
				echo "<td><SELECT name='turnid'>";

					// Printer ut turneringer i rullegardinliste
					$turnquery = "SELECT * FROM turnering ORDER BY turnid ASC";
					$turnresult = mysql_query($turnquery);
					while($t = mysql_fetch_array($turnresult))
					{
						// gjøre det enklere å hente variablene
						$turnid = $t['turnid'];
						$spill = $t['spill'];
						$format = $t['format'];
						// Skrive ut selve turneringen
						echo
						"<OPTION value='$turnid'>$turnid, $spill, $format</OPTION>";
					}
				echo "</SELECT></td>";
				echo "<td><input type='submit' name='submit' value='Vis bracket'></td>";
			echo "</tr>";
		echo "</table>";
	echo "</form>";

if(isset($turn))
{	

	if($startet > 0)
	{
		echo "<table>";
			echo "<tr>";
				echo "<td>";
					echo "<table border=1>";
						while($info = mysql_fetch_array( $q1 ))
						{

							echo "<tr>";
								echo "<th>Lagnavn</th>";
								echo "<th>Resultat</th>";
							echo "</tr>";
							echo "<tr>";
								echo "<td>".$info['Lag 1'] . "</td>";
								echo "<td>".$info['resa'] . "</td>";
							echo "</tr>";
							echo "<tr>";
								echo "<td>".$info['Lag 2'] . "</td>";
								echo "<td>".$info['resb'] . "</td>";
							echo "</tr>";
							echo "<tr>";
							echo "</tr>";
						}		
						
					echo "</table>";
				echo "</td>";
				echo "<td>";
					echo "<table border=1>";
						
						while($info = mysql_fetch_array( $q2 ))
						{							
							echo "<tr>";
								echo "<th>Lagnavn</th>";
								echo "<th>Resultat</th>";
							echo "</tr>";
							echo "<tr>";
								echo "<td>".$info['Lag 1'] . "</td>";
								echo "<td>".$info['resa'] . "</td>";
							echo "</tr>";
							echo "<tr>";
								echo "<td>".$info['Lag 2'] . "</td>";
								echo "<td>".$info['resb'] . "</td>";
							echo "</tr>";
						}
					echo "</table>";
				echo "</td>";
				
				echo "<td>";
				
					echo "<table border=1>";
				
					while($info = mysql_fetch_array( $q3 ))
					{		
						echo "<tr>";
							echo "<th>Lagnavn</th>";
							echo "<th>Resultat</th>";
						echo "</tr>";
						echo "<tr>";
							echo "<td>".$info['Lag 1'] . "</td>";
							echo "<td>".$info['resa'] . "</td>";
						echo "</tr>";
						echo "<tr>";
							echo "<td>".$info['Lag 2'] . "</td>";
							echo "<td>".$info['resb'] . "</td>";
						echo "</tr>";
					}
					echo "</table>";
				echo "</td>";
			echo "</tr>";
		echo "</table>";

	}
			
	else
	{
		echo "Turneringen har ikke startet!";
	}
	unset($turn);
}


?>


